package util;
import java.io.BufferedReader;
import java.io.File;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


/**
 * @author gubengang
 * @create 2021-09-30 19:40
 */
public final class FileUtil {
    private FileUtil(){
    }

    public static String readFile(String filePath){
        StringBuilder sb = new StringBuilder();
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sb.toString();
    }

    public static void writeFile(String filePath, String content){
        File file = new File(filePath);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try (FileWriter writer = new FileWriter(file);){
            writer.write(content);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
